<?php

namespace App\Http\Controllers\Admin;

use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
//use function GuzzleHttp\json_encode;
//use function Qiniu\json_decode;

class UserController extends Controller
{
    //分页展示
    public function index(Users $user){
        $data = $user->index();
        return view('admin.user.index',['data'=>$data]);
    }

    //删除
    public function del(Request $request,Users $users){

        $id['id'] = $request->input('id');

        $demo =  $users->del($id);

        if ($demo){
            echo "<script>alert('删除成功');</script>";
            return redirect(route("admin.user.index"));
        }else{
            echo "<script>alert('删除失败');</script>";
            return redirect(route("admin.user.index"));
        }
    }
    //添加页面
    public function create(){
        return view('admin.user.create');
    }

    //excel导出
    public function excel(){
        header("content-type:text/html;charset=utf-8");
        $dataa = DB::table('users')->get();
        $data = json_decode(json_encode($dataa),true);
//        dump($data);die;


        $str="编号\t标题\t图片\t内容\t作者\t性别\t电话\t邮箱\t建立时间\t修改时间\t删除时间"."\n";
        foreach($data as $key=>$v){
            $str.=import("\t",$v)."\n";
        }

        header("content-type:application/vnd.ms-excel");
        header("content-disposition:attachment;filename=users.xls");
        echo $str;
    }
    public function FanDiagram(Users $users){
        $demo = Users::select('sex')->get();

        foreach ($demo as $k=>$v){
            if($v['sex'] == '女士'){
                $woman[][] = $v;
            }else{
                $man[][] = $v;
            }
        }
//        $a=[
//            'woman' => count($woman),
//            'man' => count($man),
//        ];

        return view('admin.user.demo');
    }
}
